package com.quickboot.common.entity;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quickboot.common.consts.MsgConst;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.experimental.FieldNameConstants;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * 全局响应结果-表格
 *
 * @author Tellsea
 * @date 2022/11/21
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@FieldNameConstants
public class TableResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private int code;

    private String msg;

    private T data;

    private long total;

    public static <T> TableResult<T> ok(Page<T> page) {
        TableResult<T> result = new TableResult<>();
        result.setCode(HttpStatus.OK.value());
        result.setMsg(MsgConst.QUERY_SUCCESS);
        result.setData((T) page.getRecords());
        result.setTotal(page.getTotal());
        return result;
    }
}
